using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows;
using System.Xml;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace Deduplicator
{

   

 
    class ResourceFileReader
    {
        private SortedDictionary<Int64, string> fileList = new SortedDictionary<Int64, string>();
        private SortedDictionary<Int64, string> doubleList = new SortedDictionary<Int64, string>();

        private Label lblmemo;
        private PictureBox picturebox;
        private TreeView treeview;
   
        public FileCounter filecounter;
        private ProgressBar progressBar;
        public int fileCount = 0;
        public int filesDone = 0;
        public int filesMoved = 0;

        private const string cPictureExtension = ".jpg";

        private const string cMP3Extension = ".mp3";


        public ResourceFileReader(TextBox lblMusic, TextBox lblMovie, TextBox lblPicture, TextBox lblFileCount, Label lblMemo, ProgressBar progressbar, PictureBox pictureBox, TreeView treeView)
        {
            lblmemo = lblMemo;
            progressBar = progressbar;
            treeview = treeView;
            picturebox = pictureBox;
            filecounter = new FileCounter(lblMusic, lblMovie, lblPicture, lblFileCount, picturebox);
        }

        public void ScanDirectory(TreeNode Node, string DestinationFolder)
        {
            if (File.Exists(Node.Name))
                filecounter.MoveFile(Node.Name, DestinationFolder);
            else if (Directory.Exists(Node.Name))
            {
                Node.BackColor = Color.Red;
                ProcessDirectory(Node.Name, DestinationFolder);
                Node.BackColor = Color.White;
            }
        }

        public void Dispose()
        { 
            
        }

        public string GetCurrentFolder(string Subdirectory)
        {
            string[] fileparts = Subdirectory.Split('\\');
            if (fileparts.Length > 1)
                return fileparts[fileparts.Length - 1];
            else
                return "";
        }

        public int CountFiles(TreeNode Node)
        {
            try
            {
                filesDone += 1;
       
                string[] fileEntries = Directory.GetFiles(Node.Name);
                foreach (string fileName in fileEntries)
                    filecounter.AddFile(fileName);

                string[] subdirectoryEntries = Directory.GetDirectories(Node.Name);

                foreach (string subdirectory in subdirectoryEntries)
                    CountFiles(Node.Nodes.Add(subdirectory, GetCurrentFolder(subdirectory)));
            }
            catch
            {
                lblmemo.Text = "Cannot acces " + Node.Name;
                lblmemo.Update();
            }
            return fileCount;
        }

        public void ProcessDirectory(string targetDirectory, string DestinationFolder)
        {

            lblmemo.Text = targetDirectory;
            lblmemo.Update();
            try
            {
                string[] fileEntries = Directory.GetFiles(targetDirectory);
                for(int i  = 0; i < fileEntries.Length; i++)
                {
                    string fileName = fileEntries[i];
                    filecounter.MoveFile(fileName, DestinationFolder);
                 }
                string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
                foreach (string subdirectory in subdirectoryEntries)
                    ProcessDirectory(subdirectory, DestinationFolder);
            }
            catch
            {
                lblmemo.Text = "Cannot acces " + targetDirectory;
                lblmemo.Update();
            }
        }
      

        public Int64 GetFileIdentifier(FileStream fs)
        {
            Int64 fileIdentifier = 0;
            long jumpSize = fs.Length / 9;
            byte[] B = new byte[1];
            fs.Seek(0, SeekOrigin.Begin);

            for (int i = 0; i < 7; i++)
            {
                fs.Seek(jumpSize, SeekOrigin.Current);
                fs.Read(B, 0, 1);
                fileIdentifier += 256 * fileIdentifier + B[0];
            }
            return fileIdentifier;
        }
        
    }
}

    

